package com.road.vast.system.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.road.vast.common.entity.PageResult;
import com.road.vast.common.entity.Result;
import com.road.vast.common.entity.ResultCode;
import com.road.vast.controller.BaseController;
import com.road.vast.pojo.system.BsUser;
import com.road.vast.pojo.system.PeUserRole;
import com.road.vast.system.user.service.BsUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-07-13
 */
@RestController
@RequestMapping("/user")
public class BsUserController extends BaseController {
    @Resource
    BsUserService bsUserService;
    /**
     * 保存用户信息
     */
    @PostMapping("")
    public Result saveUser(@RequestBody BsUser bsUser){
        bsUser.setCompanyId(companyId);
        bsUserService.saveUser(bsUser);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/{id}")
    public Result updateUser(@RequestBody BsUser bsUser,
                             @PathVariable(value = "id") String id){
        bsUser.setId(id);
        bsUserService.updateUser(bsUser);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 通过id查询
     * @return
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable(value = "id") String id){
        System.out.println("**********");
        System.out.println(123);
        System.out.println("**********");
        BsUser user = bsUserService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(user);
        return result;
    }

    /**
     * 查询用户已经分配到的角色
     */
    @GetMapping("/simple/{id}")
    public Result simple(@PathVariable(value = "id") String id){
        List<String> roleIds = bsUserService.simple(id);
        Map<String, Object> map = new HashMap<>();
        map.put("roleIds", roleIds);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(map);
        return result;
    }

    /**
     * 动态拼接查询条件
     * @return
     */
    @GetMapping("")
    public Result findAll(int page, int size, @RequestParam Map<String, Object> map){
//        JSONObject jsonObject = JSONObject.parseObject(strMap);
//        //json对象转Map
//        Map<String, Object> map = (Map<String, Object>)jsonObject;
        map.put("companyId", companyId);
//        map.put("departmentId", "1281905018504560640");
//        map.put("hasDept", "1");
        Page<BsUser> pageUser = bsUserService.findAll(map, page, size);
        PageResult<BsUser> pageResult = new PageResult<>(pageUser.getTotal(), pageUser.getRecords());
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(pageResult);
        return result;
    }

    /**
     * 分配角色
     */
    @PutMapping("/assignRoles")
    public Result assignRoles(@RequestBody Map<String, Object> map){
        //获取被分配的用户id
        String userId = (String)map.get("id");
        //获取到角色的id列表
        List<String> roleIds =  (List<String>)map.get("roleIds");
        //调用service完成角色分配
        bsUserService.assignRoles(userId, roleIds);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除部门
     */
    @DeleteMapping("{id}")
    public Result deleteUser(@PathVariable(value = "id") String id){
        bsUserService.deleteUser(id);
        return new Result(ResultCode.SUCCESS);
    }

}

